package utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ysbang on 2015/5/29.
 */
public class RegexUtil {


        /**
         * 判断电话号码是否有效
         *
         * @param phoneNumber
         * @return true 有效 / false 无效
         */
        public static boolean isPhoneNumberValid(String phoneNumber) {

            boolean isValid = false;

            String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
            CharSequence inputStr = phoneNumber;

            Pattern pattern = Pattern.compile(expression);
            Matcher matcher = pattern.matcher(inputStr);

            if (matcher.matches()) {
                isValid = true;
            }
            return isValid;
        }

        /**
         * 判断邮箱地址是否有效
         *
         * @param email
         * @return true 有效 / false 无效
         */
        public static boolean isEmailValid(String email)
        {
            String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
            return email.matches(regex);
        }

        // 判断网络是否连接
        public static boolean isNetworkConnected(Context context) {
            if (context != null) {
                ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo mNetworkInfo = mConnectivityManager
                        .getActiveNetworkInfo();
                if (mNetworkInfo != null) {
                    return mNetworkInfo.isAvailable();
                }
            }
            return false;
        }


}
